#!/bin/bash

MANAGE=`ls manage.py`
SETTINGS=`ls settings.py`
if [ $MANAGE != "manage.py" -o $SETTINGS != "settings.py" ]; then
  echo "This script should only be executed inside the Pirate-Politics project directory."
  exit
fi

LIB=`ls ../../ | grep lib`
DJANGO=`ls | grep -E 'djangoappengine|djangotoolbox|native_tags|piston|dselector.py'`

if [ -z "$LIB" -a -z "$DJANGO" ]; then
  # remember where you started
  HERE=`pwd`

  # create the lib directory
  cd ../../
  mkdir lib
  cd lib

  # grab all of the dependencies
  hg clone http://bitbucket.org/legutierr/djangoappengine/
  hg clone http://bitbucket.org/legutierr/django-nonrel/
  hg clone http://bitbucket.org/legutierr/djangotoolbox/
  hg clone http://bitbucket.org/legutierr/django-customtags-lib/
  hg clone http://bitbucket.org/legutierr/django-tagging-nonrel/
  hg clone http://bitbucket.org/offline/django-annoying/
  hg clone http://bitbucket.org/jespern/django-piston/
  hg clone http://bitbucket.org/jmoiron/django-selector/
  hg clone http://bitbucket.org/fragro/markdown
  hg clone http://bitbucket.org/fragro/beautifulsoup


  # go back to where you started
  cd $HERE

  # create the required symbolic links
  ln -s ../../lib/djangoappengine
  ln -s ../../lib/django-nonrel/django
  ln -s ../../lib/djangotoolbox/djangotoolbox
  ln -s ../../lib/django-customtags-lib/customtags
  ln -s ../../lib/django-tagging-nonrel/tagging
  ln -s ../../lib/django-annoying/annoying
  ln -s ../../lib/django-piston/piston
  ln -s ../../lib/django-selector/dselector.py
  ln -s ../../lib/python-markdown/markdown
  ln -s ../../lib/arkdown/python-markdown/markdown
  ln -s ../../lib/beautifulsoup/BeautifulSoup.py 


  # add the app.yaml file
  cp app.yaml.example app.yaml

  # run syncdb
  python manage.py syncdb
fi
